package com.dq.service.sys.user.centre.impl;

import com.dq.controller.in.user.UserPlanQuery;
import com.dq.controller.in.user.UserSummaryQuery;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.sys.user.Account;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.UserPlan;
import com.dq.domain.sys.user.user.centre.UserSummary;
import com.dq.exception.customException.AdminException;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.user.centre.UserSummaryService;
import com.dq.utils.TimeUtil;
import com.dq.vo.Vo;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/4/10 14:09
 */
@Service
public class UserSummaryServiceImpl extends BaseRepositories implements UserSummaryService {
    @Override
    public List<UserSummary> list(UserSummaryQuery userSummaryQuery) throws AdminException {
        Map<String, Object> query = createQuery(userSummaryQuery);

        return dao.find(dao.query2Hql(UserSummary.class, query), query);
    }
    @Override
    public Long count(UserSummaryQuery userSummaryQuery) throws AdminException {
        Map<String, Object> query = createQuery(userSummaryQuery);

        return dao.count(dao.count2Hql(UserSummary.class, query), query);
    }
    public Map<String, Object> createQuery(UserSummaryQuery userSummaryQuery){
        Map<String, Object> query = userSummaryQuery.parseMap();
        if(userSummaryQuery.userId != null) {
            query.put("createUser_id", userSummaryQuery.getUserId());
            query.remove("userId");
        }

        return query;
    }


    @Override
    public Vo save(UserSummary userSummary) throws AdminException {
        SysUser loginUser = sysUserService.getUser();
        Calendar now = Calendar.getInstance();
        Long year = new Long(now.get(Calendar.YEAR));
        Long month = new Long(now.get(Calendar.MONTH) + 1);
        Long day = new Long(now.get(Calendar.DAY_OF_MONTH));
        UserSummary select = userSummaryRepository.findByDeletedFalseAndCreateUserAndYearAndMonthAndDay(loginUser,year,month,day);
        if(select != null){
            throw new AdminException("今日已提交过");
        }
        if(loginUser.getSection() == null || loginUser.getSection().getId() == null) {
            throw new AdminException("登录账号所属门店信息异常");
        }
        userSummary.setSection(loginUser.getSection());
        userSummary.setCreateUser(loginUser);
        userSummary.setUpdateUser(loginUser);
        userSummary.setYear(year);
        userSummary.setMonth(month);
        userSummary.setDay(day);
        userSummaryRepository.save(userSummary);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo list(Long staffUserId) throws AdminException {
        BossUser staffUser = dao.get(BossUser.class,staffUserId);
        if(staffUser == null){
            throw new AdminException("用户异常");
        }
        //目标业绩
        BigDecimal targt = BigDecimal.ZERO;
        Calendar now = Calendar.getInstance();
        UserPlan userPlan = userPlanRepository.findByDeletedFalseAndYearAndMonthAndBossUser(new Long(now.get(Calendar.YEAR)),new Long(now.get(Calendar.MONTH) + 1),staffUser);
        if(userPlan != null && null != userPlan.getTarget()){
            targt = userPlan.getTarget();
        }
        Map<String, Object> map = completeMap(staffUserId, targt);
        return new Vo(map);
    }

    @Override
    public  Map<String,Object> completeMap(Long staffUserId, BigDecimal targt) {
        Map<String,Object> map = new HashMap<>(16);
        //本月已完成
        BigDecimal completedByMonth = userSummaryRepository.countCompleted(staffUserId, TimeUtil.getThisMonthFirstDayZeroTimestamp());
        //本日已完成
        //服务产生的
        BigDecimal completedByToday = subscribeRepository.countCompleted(staffUserId, Subscribe.SubscribeStatus.serviced, TimeUtil.getTodayZeroTimestamp());
        //充卡产生的
        BigDecimal rechargeByToday = BigDecimal.ZERO;
        if(accountRepository.SumRecharge(staffUserId,TimeUtil.getThisMonthFirstDayZeroTimestamp(), Account.ChangeType.add) != null){
                rechargeByToday = accountRepository.SumRecharge(staffUserId,TimeUtil.getThisMonthFirstDayZeroTimestamp(), Account.ChangeType.add);
        }
        //剩余未完成
        BigDecimal incomplete = BigDecimal.ZERO;
        if(targt.compareTo(completedByMonth) == 1){
            incomplete = targt.subtract(completedByMonth);
        }
        map.put("tragt",targt);
        map.put("completedByMonth",completedByMonth);
        map.put("completedByToday",completedByToday.add(rechargeByToday));
        map.put("incomplete",incomplete);

        return map;
    }

    public static void main(String[] args) {
                Calendar now = Calendar.getInstance();

                System.out.println("年：" + now.get(Calendar.YEAR));

                System.out.println("月：" + (now.get(Calendar.MONTH) + 1));

                System.out.println("日：" + now.get(Calendar.DAY_OF_MONTH));

                System.out.println("时：" + now.get(Calendar.HOUR_OF_DAY));

                System.out.println("分：" + now.get(Calendar.MINUTE));

                System.out.println("秒：" + now.get(Calendar.SECOND));
    }
}
